package training02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Training {
    public void subject1(){
        Map<Integer,String> map = new HashMap<Integer, String>();
        map.put(1, "Amy");
        map.put(2, "Joe");
        map.put(3, "Tom");
        map.put(4, "Susan");
        // 1.遍历集合，并将序号与对应人名打印。
        for(Integer index : map.keySet()){
            System.out.println(index + " " + map.get(index));
        }

        // 2.向该map集合中插入一个编码为5姓名为控制台输入的人名的信息
        Scanner sc = new Scanner(System.in);
        System.out.println("输入人名：");
        String name = sc.next();
        map.put(5,name);

        // 3.移除该map中的编号为4的信息
        map.remove(4);

        // 4.将map集合中编号为3的姓名信息修改为"Tommy"
        map.put(3,"Tommy");

        // 5.再次遍历经过上述操作后的集合，并将序号与对应人名打印。(注：第一次输出和第二次输出需用空行隔开)
        for(Integer index : map.keySet()){
            System.out.println(index + " " + map.get(index));
        }

    }

    public void subject2(){
        // 给定["a", "b", "a", "b", "c", "a", "b", "c", "b"]字符串数组，
        // 然后使用Map的key来保存数组中字符串元素，value保存该字符串元素的出现次数，
        // 最后统计出各字符串元素的出现次数。
        String [] strs = {"a", "b", "a", "b", "c", "a", "b", "c", "b"};
        Map<String,Integer> map = new HashMap();
        for(String s : strs){
            if (map.containsKey(s)) {
                map.put(s,map.get(s)+1);
            }else{
                map.put(s,1);
            }
        }
        System.out.println(map);
    }
    //编写一个方法，实现读取一个二进制文件，将其转换为十六进制格式输出。
    public void binaryToHex(String filePath){
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            int b = 0;
            while((b = fis.read()) != -1){
                System.out.print(Integer.toHexString(b));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public void copy(String srcPath,String distPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcPath);
            fos = new FileOutputStream(distPath);
            byte [] bs = new byte[1024];
            int len = 0;
            while((len = fis.read(bs)) != -1){
                fos.write(bs,0,len);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(fis != null ){
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(fos != null ){
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public static void main(String[] args) {
        Training training = new Training();
//        training.subject1();
//        training.subject2();
//        training.binaryToHex("1.txt");
        training.copy("D:\\Pictures\\vscode_bak.png","D:\\1.png");
    }
}
